﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2018-12-26 11:54:00
// 负责人：lvsc
// ===================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using System.Text;
namespace OF.Component.DAL
{
	/// <summary>
	///用户来源 数据访问层
	/// </summary>	
	internal partial class UserSource:IUserSource
	{		
    
         //连接字符串
         public virtual string ConnectionStringKey
         {
             get
             {
                 return ConstantUtility.CGTConnectionString;
             }
         }
           
         ///<summary>
         /// 获取最大主键ID，针对非自增列主键 
         /// 暂时无用
         ///</summary>
         public int GetMaxID()
         {
             int nReturn=0;
             string szSql=@"SELECT ISNULL(MAX(UserSourceID),0)+1
                            FROM [UserSource]";             
             Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
             DbCommand dbc = db.GetSqlStringCommand(szSql);
             try
             {
                 object o = db.ExecuteScalar(dbc);
                 if (o!=null)
                 {
                     nReturn = Convert.ToInt32(o);
                 }
             }
             catch (Exception ex)
             {
                 throw ex;
             }
             return nReturn;               
         }
                
     
         ///<summary>
         /// datareader转对象
         ///</summary>
         private UserSourceInfo GetByReader(IDataReader dr)
         {
            UserSourceInfo info = new UserSourceInfo();
            object obj;
            obj=dr["UserSourceID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.UserSourceID = Convert.ToInt32(obj);
            }
            obj=dr["UserID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.UserID = (long)obj;
            }
            obj=dr["SourceUserName"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.SourceUserName = obj.ToString();
            }
            obj=dr["SourceUserHead"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.SourceUserHead = obj.ToString();
            }
            obj=dr["SourceToken"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.SourceToken = obj.ToString();
            }
            obj=dr["SourceType"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.SourceType = Convert.ToInt32(obj);
            }
            obj=dr["CreatedDate"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.CreatedDate = Convert.ToDateTime(obj);
            }
            obj=dr["IsActive"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.IsActive = Convert.ToInt32(obj);
            }
            return info;
         }
         
        ///<summary>
        /// 插入接口
        ///</summary>
		public int Insert(UserSourceInfo info)
		{
            string strSql=@"INSERT INTO [UserSource]			
			(			
			[UserID],
			[SourceUserName],
			[SourceUserHead],
			[SourceToken],
			[SourceType],
			[CreatedDate],
			[IsActive]
			)
			 VALUES
			(			
			@UserID,
			@SourceUserName,
			@SourceUserHead,
			@SourceToken,
			@SourceType,
			@CreatedDate,
			@IsActive
			)
			 SET @UserSourceID=@@IDENTITY
			";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql);
            db.AddOutParameter(dbc, "@UserSourceID",DbType.Int32, sizeof(Int32));  
            db.AddInParameter(dbc, "@UserID",DbType.Int64, info.UserID);
            db.AddInParameter(dbc, "@SourceUserName",DbType.String, info.SourceUserName);
            db.AddInParameter(dbc, "@SourceUserHead",DbType.String, info.SourceUserHead);
            db.AddInParameter(dbc, "@SourceToken",DbType.String, info.SourceToken);
            db.AddInParameter(dbc, "@SourceType",DbType.Int32, info.SourceType);
            db.AddInParameter(dbc, "@CreatedDate",DbType.DateTime, info.CreatedDate.ToNullDbDateTime());
            db.AddInParameter(dbc, "@IsActive",DbType.Int32, info.IsActive);

             
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
            return (int)db.GetParameterValue(dbc,"UserSourceID");
         
		}
		
		///<summary>
        /// 修改接口
        ///</summary>
        public void Update(UserSourceInfo info)
		{
            StringBuilder sbSql=new StringBuilder("UPDATE [UserSource] SET ");
            sbSql.AppendLine("[UserID] = @UserID,");
                
            sbSql.AppendLine("[SourceUserName] = @SourceUserName,");
                
            sbSql.AppendLine("[SourceUserHead] = @SourceUserHead,");
                
            sbSql.AppendLine("[SourceToken] = @SourceToken,");
                
            sbSql.AppendLine("[SourceType] = @SourceType,");
                
            sbSql.AppendLine("[CreatedDate] = @CreatedDate,");
                
            sbSql.AppendLine("[IsActive] = @IsActive");
                
            sbSql.Append(" where  [UserSourceID] = @UserSourceID");
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(sbSql.ToString());
            db.AddInParameter(dbc, "@UserSourceID",DbType.Int32, info.UserSourceID);
            db.AddInParameter(dbc, "@UserID",DbType.Int64, info.UserID);
            db.AddInParameter(dbc, "@SourceUserName",DbType.String, info.SourceUserName);
            db.AddInParameter(dbc, "@SourceUserHead",DbType.String, info.SourceUserHead);
            db.AddInParameter(dbc, "@SourceToken",DbType.String, info.SourceToken);
            db.AddInParameter(dbc, "@SourceType",DbType.Int32, info.SourceType);
            db.AddInParameter(dbc, "@CreatedDate",DbType.DateTime, info.CreatedDate.ToNullDbDateTime());
            db.AddInParameter(dbc, "@IsActive",DbType.Int32, info.IsActive);
			
           
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
		}
		
        ///<summary>
        /// 删除接口
        ///</summary>
        public void Delete(int userSourceID)
		{
			StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM [UserSource]  ");
            strSql.Append("WHERE [UserSourceID]=@UserSourceID ");
           
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
		    db.AddInParameter(dbc, "@UserSourceID",DbType.Int32, userSourceID);
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
		}
        
         ///<summary>
         /// 搜索接口
         ///</summary>
         public IList<UserSourceInfo> Search(out int pageCount, int pageIndex, int pageSize, string where, string orderField, bool isDesc)
         {
            IList<UserSourceInfo> list = new List<UserSourceInfo>();
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetStoredProcCommand("sp_OF_Page");
            //设置参数
            db.AddOutParameter(dbc, "@pageCount", DbType.Int32, 4);
            db.AddInParameter(dbc, "@pageIndex", DbType.Int32, pageIndex);
            db.AddInParameter(dbc, "@pageSize", DbType.Int32, pageSize);
            db.AddInParameter(dbc, "@strWhere", DbType.String, where);
            db.AddInParameter(dbc, "@fldName", DbType.String, orderField);
            db.AddInParameter(dbc, "@OrderType", DbType.Boolean, isDesc);
            db.AddInParameter(dbc, "@tblName", DbType.String, "[UserSource]");
            db.AddInParameter(dbc, "@strGetFields", DbType.String, "*");
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                while (dr.Read())
                {
                    UserSourceInfo info = GetByReader(dr);
                    list.Add(info);
                }
            }
            pageCount = (int)db.GetParameterValue(dbc, "@PageCount");
            return list;
         }
        
      
        ///<summary>
        ///根据主键取出信息
        ///</summary>
        public UserSourceInfo GetByID(int userSourceID)
		{
            Model.UserSourceInfo  info = null;
		    StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT  * FROM [UserSource] ");
            strSql.Append("WHERE [UserSourceID]=@UserSourceID ");
           
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
		    db.AddInParameter(dbc, "@UserSourceID",DbType.Int32, userSourceID);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                if (dr.Read())
                {
                    info = GetByReader(dr);
                }
            }
            return info;
		}	
	}
}